package com.czl.module_user.fragment

import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.util.ObjectUtils
import com.blankj.utilcode.util.StringUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.ChoiceImgHelper
import com.czl.base.util.DialogHelper
import com.czl.module_user.BR
import com.czl.module_user.R
import com.czl.module_user.databinding.FragmentAddPeopleBinding
import com.czl.module_user.viewmodel.AddPeopleViewModel
import java.util.*

/**
 *
 * @Description:添加人员
 * @Author: XCH
 * @CreateDate: 2022/1/7 8:39
 */
@Route(path = AppConstants.Router.User.F_ADD_PEOPLE)
class AddPeopleFragment : BaseFragment<FragmentAddPeopleBinding, AddPeopleViewModel>() {

    override fun initContentView(): Int {
        return R.layout.fragment_add_people
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        if (arguments != null) {
            viewModel.peopleId = requireArguments().getString(AppConstants.BundleKey.PEOPLE_ID, "")
            viewModel.peopleFrom =
                requireArguments().getInt(AppConstants.BundleKey.PEOPLE_FROM, AddPeopleViewModel.FROM_ADD)
        }
        if (StringUtils.isEmpty(viewModel.peopleId)) {
            viewModel.tvTitle.set("添加人员")
        } else {
            viewModel.tvTitle.set("人员详情")
            reload()
        }
        viewModel.btnText.set(
            when (viewModel.peopleFrom) {
                AddPeopleViewModel.FROM_EDIT -> "重新保存"
                else -> "保存"
            }
        )
        viewModel.getUserRooms()
    }

    override fun initViewObservable() {

        viewModel.uc.choiceGenderEvent.observe(this, {
            DialogHelper.showBottomListDialog(
                requireContext(),
                "请选择",
                viewModel.personGenders
            ) { position: Int, _: String? ->
                viewModel.gender.set(position)
            }
        })
        viewModel.uc.choiceHouseEvent.observe(this, {
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(it)
                    .filter { t -> t.isHead }
                    .map { t -> t.houseCode }
                    .withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position: Int, text: String ->
                viewModel.houseName.set(text)
                viewModel.houseNo = it[position].houseId
            }
        })

        viewModel.uc.choicePersonTypeEvent.observe(this, {
            DialogHelper.showBottomListDialog(
                requireContext(),
                "请选择", viewModel.personTypes
            ) { position: Int, text: String ->
                viewModel.personTypeName.set(text)
                viewModel.personType = position.plus(2).toString()
            }
        })

        viewModel.uc.saveCompleteEvent.observe(this, {
            back()
            LiveBusCenter.postRefreshPeopleEvent()
        })

        viewModel.uc.deleteEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定删除该人员吗？") {
                viewModel.deleteHouseUser(it)
            }
        })

        viewModel.uc.deleteSuccessEvent.observe(this, {
            back()
        })

        viewModel.uc.choiceImgEvent.observe(this, {
            ChoiceImgHelper.ChoiceImg(this,
                { result -> result[0]?.let { t -> viewModel.uploadHeadImg(t.compressPath, it) } },
                {})
        })
    }


    override fun reload() {
        super.reload()
        viewModel.getUserDetail()
    }

}